package com.nanfeng.screenshot.controller;

import com.nanfeng.screenshot.enums.ChengduHousingEnum;
import com.nanfeng.screenshot.service.ScreenshotService;
import com.nanfeng.screenshot.service.ScreenshotSimpleService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;


/**
 * @ClassName：ScreenshotController
 * @Author: LiuYunZhao
 * @Date: 2025/7/11 9:01
 * @Description: 截图工具控制层
 */
@RestController
@RequestMapping("/api/screenshot")
// 启用定时任务功能
@EnableScheduling
public class ScreenshotController {

    @Resource
    private ScreenshotSimpleService screenshotSimpleService;
    @Resource
    private ScreenshotService screenshotService;

    /**
     * 每1分钟执行一次
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    public void scheduledScreenshotTask() {
        try {
            // 定义要访问的网站列表
            List<String> urls = Arrays.asList(
                    "https://www.baidu.com",
                    "https://www.bing.com",
                    "https://www.163.com"
            );

            // 执行截图并压缩
            String zipFilePath = screenshotSimpleService.saveScreenshotAndZipSimple(urls);
            System.out.println("定时任务执行完成，截图已保存至: " + zipFilePath);

        } catch (Exception e) {
            System.err.println("定时截图任务执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 每1分钟执行一次
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void scheduledScreenshotPlusTask() {
        try {
            // 执行截图并压缩
            String zipFilePath = screenshotService.saveScreenshotAndZip(ChengduHousingEnum.list());
            System.out.println("定时任务执行完成，截图已保存至: " + zipFilePath);

        } catch (Exception e) {
            System.err.println("定时截图任务执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}    